#!/usr/bin/env expect
############################################################################
# Purpose: Test of POE functionality
#          Test of hostfile option (-hostfile).
#
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
#
############################################################################
# Copyright (C) 2002-2006 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Danny Auble <da@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
#
# This file is part of Slurm, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Slurm; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set test_id     "11.7"
set exit_code   0
set env(SLURM_LL_API_DEBUG) "2"
set num_nodes   2
set num_tasks   2
set node_count  0
set task_count  0
set hostfile    "test$test_id.hostfile"

print_header $test_id

exec $bin_rm -f $hostfile

if { ![test_launch_poe] } {
	send_user "\nWARNING: This test is only compatible with launch/poe systems\n"
	exit $exit_code
}

if { [string length $partition] == 0 } {
	set partition [default_partition]
}

#find out if we have enough nodes to test functionality
set node_count [get_node_cnt_in_part $partition]
if { $node_count < 2 } {
	send_user "WARNING: system must have at least 2 \
		   nodes to run this test on.  This system \
		   only has $node_count.\n"
	exit $exit_code
}

set node0 0
set node1 0

for {set i 0} {$i<2} {incr i} {

	if { $i==1 } {
		if { $node0 == 0 || $node1 == 0 } {
			send_user "\nFAILURE: node names not set from \
					previous poe run\n"
			exit 1
		}
		set 1node0 $node0
		set 1node1 $node1
		set file [open $hostfile "w"]
		puts $file "$node1\n$node0"
		close $file
		spawn $poe $bin_printenv MP_I_UPMD_HOSTNAME -labelio yes \
				-procs $num_tasks -nodes $num_nodes \
				-hostfile $hostfile -resd yes \
		                -stdoutmode ordered
	} else {
		#
		# execute poe with a specific node count
		#
		spawn $poe $bin_printenv MP_I_UPMD_HOSTNAME -labelio yes \
				-procs $num_tasks -nodes $num_nodes \
				-rmpool slurm -stdoutmode ordered
	}
	expect {
		-re "0:($alpha_numeric_under)" {
			set node0 $expect_out(1,string)
			exp_continue
		}
		-re "1:($alpha_numeric_under)" {
			set node1 $expect_out(1,string)
			exp_continue
		}
		timeout {
			send_user "\nFAILURE: poe not responding\n"
			set exit_code 1
		}
		eof {
			wait
		}
	}
}
if { [string compare $node0 $1node1] } {
	send_user "\nFAILURE: tasks not distributed by hostfile\n"
	set exit_code 1
}
if { [string compare $node1 $1node0] } {
	send_user "\nFAILURE: tasks not distributed by hostfile\n"
	set exit_code 1
}
if {$exit_code == 0} {
	send_user "\nSUCCESS\n"
}
exit $exit_code
